/*
 *  CEListEditDlg.h
 *  LID Library
 *
 *  Created by Alexander Trutchenko on 09/11/11.
 *  Copyright 2011 League of Independent Developers. All rights reserved.
 *
 */

#ifndef __CE_ListEditDlg_h__
#define __CE_ListEditDlg_h__

#include "cocos2dx_extentions/controls/CEModalView.h"
#include "cocos2dx_extentions/labels/CELabelProtocol.h"
#include "lid_lib/src/types/LLTypes.h"

#include <map>
#include <list>
#include <string>

namespace cocosext {

#define CE_LIST_EDIT_VIEW_SCREEN_MARGIN 15.f

/////////////////////////////////////////////////////
//
/** CETextListItemFactory 
* This interface is used to create list items for CEListEditDlgInfo listView
*/
/////////////////////////////////////////////////////

class CEVerticalListView;

class CEListProcessor
{
protected:
	float _itemWidth;

public:
	CEListProcessor(float itemWidth): _itemWidth(itemWidth) {;}
	virtual ~CEListProcessor() {;}

public: 
	virtual void fillListViewWithMask(const std::wstring & mask, CEVerticalListView* list, cocos2d::CCObject* target, cocos2d::SEL_MenuHandler selector) = 0;
	virtual const wchar_t* getTextOnListItemPressed(cocos2d::CCObject* caller) = 0;
};

/////////////////////////////////////////////////////
//
/** CEListEditDlgInfo 
* Struct with CEListEditDlg description data
*/
/////////////////////////////////////////////////////

class CEVerticalListView;
class CELabelProtocol;

struct CEListEditDlgInfo
{
	cocos2d::CCNode* wnd;
	cocos2d::CCNode* title;
	cocos2d::CCNode* editBk;
	cocos2d::CCNode* label;
	cocos2d::CCNode* closeBtnSprNorm;
	cocos2d::CCNode* closeBtnSprPressed;
	cocos2d::CCNode* listBk;
	CEVerticalListView* list;
	CEListProcessor* listProcessor;

	float titleAreaHeight;
	float titleMarginLeft;
	float editAreaHeight;
	float labelMarginLeft;
	float listBkMargin;
	float listMargin;
	float listMarginLeft;
    
    bool isRetinaOnIpad;

public:
	CEListEditDlgInfo()
		: wnd(NULL) 
		, title(NULL)
		, editBk(NULL)
		, label(NULL) 
		, closeBtnSprNorm(NULL) 
		, closeBtnSprPressed(NULL)
		, listBk(NULL)
		, list(NULL)
		, listProcessor(NULL)
		, titleAreaHeight(10.f)
		, titleMarginLeft(titleAreaHeight/2.f)
		, editAreaHeight(10.f)
		, labelMarginLeft(0.f)
		, listBkMargin(0.f)
		, listMargin(0.f)
		, listMarginLeft(0.f)
        , isRetinaOnIpad(false)
		{;}
};

/////////////////////////////////////////////////////
//
/** CEListEditDlg 
* CEListEditDlg set color/opacity to all its children recursivly
*/
/////////////////////////////////////////////////////

class CEListEditDlg : public CEModalView, public cocos2d::CCIMEDelegate
{
protected:
	CENodeRGBA* _wnd;
	cocos2d::CCNode* _bk;
	cocos2d::CCNode* _title;
	cocos2d::CCNode* _editBk;
	cocos2d::CCNode* _listBk;
	cocos2d::CCMenu* _btnMenu;
	std::string		_utf8text;
	CELabelProtocol* _label;

	CEVerticalListView* _listView;
	CEListProcessor* _listProcessor; //Strong ref
    
    bool _isRetinaOnIpad;

protected:
	CEListEditDlg()
		: CEModalView(true, true)
		, _wnd(NULL)
		, _bk(NULL)
		, _title(NULL)
		, _editBk(NULL)
		, _listBk(NULL)
		, _btnMenu(NULL)
		, _label(NULL)
		, _listView(NULL)
		, _listProcessor(NULL)
		, _isRetinaOnIpad(false) {;}

public:
	virtual ~CEListEditDlg() { delete _listProcessor; }

	static bool createAndAttachToCurrentScene(const CEListEditDlgInfo& info, const cocos2d::ccColor4B& color);

	virtual bool init(const CEListEditDlgInfo& info, const cocos2d::ccColor4B& color);

public:
// Main interface
	const wchar_t* currentText() const { return _label->getString(); }
	const CEListProcessor* listProcessor() const { return _listProcessor; }

// CCIMEDelegate interface
	virtual bool attachWithIME();
    virtual bool detachWithIME();

protected:
	virtual bool canAttachWithIME() const { return true; }
    virtual bool canDetachWithIME() const { return true; }
    virtual void insertText(const char * text, int len);
    virtual void deleteBackward();
	virtual const char * getContentText() const { return _utf8text.c_str(); }

    virtual void keyboardWillShow(cocos2d::CCIMEKeyboardNotificationInfo& info);
    virtual void keyboardDidShow(cocos2d::CCIMEKeyboardNotificationInfo& info);
    virtual void keyboardWillHide(cocos2d::CCIMEKeyboardNotificationInfo& info);
    virtual void keyboardDidHide(cocos2d::CCIMEKeyboardNotificationInfo& info);

protected:
	virtual void close();
	virtual void finishClose();
	virtual void finishOpen();
	virtual void setMarginBottom(float marginBottom);

private:
	void resetListView();

	void onListItemPressed(cocos2d::CCObject* caller);
	void onCancel(cocos2d::CCObject* caller);

	void closeWithDelay(cocos2d::ccTime time);
};


}; // end namespace cocosext

#endif // __CE_ListEditDlg_h__
